package com.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.dao.UserDAO;
import com.model.User;
import com.util.DB;

public class UserDAOImpl implements UserDAO {

	// 连接数据库
	// private static Connection conn = DB.getConnection();
	private Connection conn = DB.con;

	// 注册用户
	@Override
	public boolean addUser() {
		// TODO Auto-generated method stub
		return false;
	}

	// 删除用户
	@Override
	public boolean deleteUser() {
		// TODO Auto-generated method stub
		return false;
	}

	// 更改用户
	@Override
	public boolean updateUser() {
		// TODO Auto-generated method stub
		return false;
	}

	// 用户登录
	@Override
	public boolean loginUser(User user) {
		// TODO Auto-generated method stub
		PreparedStatement st = null;
		ResultSet rs = null;
		try {

			st = conn
					.prepareStatement("select * from user where USERNAME = ? and PASSWORD = ?");
			st.setString(1, user.getUserName());
			st.setString(2, user.getPassWord());
			rs = st.executeQuery();
			if (rs.next()) {
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			DB.close(st, null);
		}
		return false;

	}

	// 注销用户
	@Override
	public void logoutUser(String user) {
		// TODO Auto-generated method stub
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		session.removeAttribute(user);
	}

}
